require.config({
	paths: {
		"jquery": "jquery",
		"common": "xz-common",
		"cookie": "jquery.cookie",
	}
})
require(["common", "jquery", "cookie"], function(common, $) {
	$(function() {
    //注册页跳转
    $("#toresgister").mousedown(function(){
    	location.href = "html/register.html"
    
    });
	
    //购物车跳转
     $("#xz-shopcar").mousedown(function(){
    	location.href = "html/shopcar.html"
    	
    });
    //列表页跳转
     $(".xz-listofgoods-one a").mousedown(function(){
     	location.href = "html/listgoods.html"
     	
     })
		$pot = 0; //取消二级菜单效果
    //是否登录
        if($.cookie("login")){
        	$("#xz-login").text($.cookie("login")+" ，点击重新登录");
        }else{
        	$("#xz-login").text("【登录】");
        }
     //退出
      $("#xz-login").mousedown(function(){
      	$.removeCookie("login",{expires:0,path:"/"});
      	location.href = "html/login.html";
      })
		//轮播效果
		$index = 0;
		$("#xz-banner").hover(
			function() {
				$("#xz-banner-right").animate({
						right: "30px",
						opacity: "1"
					}, 300),
					$("#xz-banner-left").animate({
						left: "30px",
						opacity: "1"
					}, 300)
			},
			function() {
				//	autoplay();
				$("#xz-banner-right").animate({
						right: "-30px",
						opacity: "0"
					}, 300),
					$("#xz-banner-left").animate({
						left: "-30px",
						opacity: "0"
					}, 300)
			})
		//初始化界面 
		showphoto();
		//显示图片方法
		function showphoto() {
			$("#xz-banner .xz-banner-a").eq($index).fadeIn(1000);
			$("#xz-banner-pointer .xz-banner-pointer-a").eq($index).css({
				"background-position-y": "-118px"
			})
		}
		//隐藏图片方法
		function hidephoto() {
			$("#xz-banner .xz-banner-a").eq($index).hide();
			$("#xz-banner-pointer .xz-banner-pointer-a").eq($index).css({
				"background-position-y": "-137px"
			})
		}
		var timer1 = setInterval(
			function() {
				hidephoto();
				$index++;
				if($index == 4) {
					$index = 0;
				}
				showphoto();

			}, 3000)
		$("#xz-banner").mouseover(function() {
			clearInterval(timer1);
		})

		$("#xz-banner").mouseout(
			function() {
				clearInterval(timer1);
				var timer1 = setInterval(
					function() {
						hidephoto();
						$index++;
						if($index == 4) {
							$index = 0;
						}
						showphoto();

					}, 3000)
				$("#xz-banner").mouseover(function() {
					clearInterval(timer1);
				})
			})

		$("#xz-banner-pointer .xz-banner-pointer-a").each(
			function(i) {
				$("#xz-banner-pointer .xz-banner-pointer-a").eq(i).mousedown(
					function() {
						$("#xz-banner-pointer .xz-banner-pointer-a").each(
							function(j) {
								$index = j;
								hidephoto();

							})
						$index = i;
						showphoto();
					})
			})

		//左按钮
		$("#xz-banner-left").mousedown(
			function() {
				hidephoto();
				$index--;
				if($index < 0) {
					$index = 3;
				}
				showphoto();
			}
		)
		//右按钮
		$("#xz-banner-right").mousedown(
			function() {
				hidephoto();
				$index++;
				if($index > 3) {
					$index = 0;
				}
				showphoto();
			}
		)

	}) //轮播图按钮	 
	$(function() {
	
		$(".xz-main-content-warp1-son").each(
			function(i) {
				$(".xz-main-content-warp1-son").eq(i).hover(
					function() {
						$(".xz-main-content-warp1-son").eq(i).css({
							"box-shadow": "0 0 30px #000"
						})
					},
					function() {
						$(".xz-main-content-warp1-son").eq(i).css({
							"box-shadow": "0 0 0 #000"
						})
					}
				)
			})
	}) //阴影效果

	//倒计时
	function showtime() {
		setInterval(function() {
			var timer_start = new Date();
			$.getJSON("xz.json", function(json) {
				var timer_end = new Date();
				timer_end.setMonth(json.timer.goodstimemon);

				timer_end.setDate(json.timer.goodstimedate);
				timer_end.setHours(json.timer.goodstimehours);
				timer_end.setMinutes(json.timer.goodstimemintue);
				timer_end.setSeconds(json.timer.goodstimesecond);
				var timergap = parseInt((timer_end - timer_start) / 1000);
				var seconds = timergap % 60;
				if(seconds < 10) {
					seconds = "0" + seconds;
				}
				var minutes = ((timergap - seconds) / 60) % 60;
				if(minutes < 10) {
					minutes = "0" + minutes;
				}
				var hours = ((((timergap - seconds) / 60) - minutes) / 60) % 24;
				if(hours < 10) {
					hours = "0" + hours;
				}
				var dates = parseInt(timergap / 60 / 60 / 24);
				if(dates < 10) {
					dates = "0" + dates;
				}
				$(".xz-main-content-timer").html("仅剩：" + dates + "天" + hours + "时" + minutes + "分" + seconds + "秒")
			});
		}, 1000)
	}
	showtime();
     
    	//第三部分
	$(function() {
		$.getJSON("xz.json", function(json) {
//			$(".xz-main-content-warp3-son").each(
//				function(i) {
//					$(".xz-main-content-warp3-son  #xz-main-warp3-small").eq(i).html(json.main_son3[i].productname);
//					$(".xz-main-content-warp3-son .xz-content-salepirce1 .xz-content-salepirce-now1").eq(i).html(json.main_son3[i].now_pirce);
//					$(".xz-main-content-warp3-son .xz-content-salepirce1 .xz-content-salepirce-old1").eq(i).html(json.main_son3[i].old_pirce)
//					$(".xz-main-content-warp3-son #xz-main-warp3-logo1 ").eq(i).html(json.main_son3[i].discount);
//					$(".xz-main-content-warp3-son .xz-main-describe").eq(i).html(json.main_son3[i].productdescribe);
//					$(".xz-main-content-warp3-son .xz-main-content-warp3-son-a img").eq(i).attr({
//						"src": json.main_son3[i].productphoto
//					})
//				})
          for (i in json.main_son3){
          	$(".xz-main-content-warp3-h2").after(
          	'<div class="xz-main-content-warp3-son">'
		   +'<a href="#" class="xz-main-content-warp3-son-a">'
		   	+'<img src="'+json.main_son3[i].productphoto+'"/>'
		   	+'<span id = "xz-main-content-warp3-grandson">真心便宜</span>'
		   	+'</a>'
		   	+'<a href="#" id="xz-main-warp3-small">'+json.main_son3[i].productname+'</a>'
		   +'<p class="xz-main-describe" >'+json.main_son3[i].productdescribe+'</p>'
		   	 +' <div class="xz-content-salepirce1">'
		   	  	  + '<span class="xz-content-salepirce-now1">'+json.main_son3[i].now_pirce+'</span>'
		   	  	  +' <span class="xz-content-salepirce-old1">'+json.main_son3[i].old_pirce+'</span>'
		   	  	  +'<a herf="#" class="xz-content-but">立即抢购</a>'
		   	  	  +'<span id="xz-main-warp3-logo1">'+json.main_son3[i].discount+'</span>'
		   	  	  + '<p id="xz-main-warp3-end">'
		   	  	  +  	'148 <span>已购买</span>'
		   	  	  + ' </p>'
		   	  	  +'</div>'
		   +'</div>'
          	)
          }
		})
		
	//热销推荐动态效果
        $("body").mouseover(function(){
        	$(".xz-main-content-warp3-son").each(function(i) {
			$(".xz-main-content-warp3-son").eq(i).hover(function() {
				$(".xz-main-content-warp3-son").eq(i).css({
					"box-shadow": "0 0 20px #000 ",
					"z-index": "10"
					
				});
			}, function() {
				$(".xz-main-content-warp3-son").eq(i).css({
					"box-shadow": "0 0 0px #000",
					"z-index": "0"

				})
			})
		})
        	
        })



	}) 
	// 品牌特卖动画效果
	$(function() {

		$("#xz-main-content-warp1-grandson .xz-main-content-warp1-grandson1").each(
			function(i) {
				$("#xz-main-content-warp1-grandson .xz-main-content-warp1-grandson1").eq(i).hover(
					function() {
						$("#xz-main-content-warp1-grandson .xz-main-content-warp1-grandson1 #xz-main-content-warp1-grandson2").eq(i).animate({
							bottom: "0"
						}, 300)
					},
					function() {
						$("#xz-main-content-warp1-grandson .xz-main-content-warp1-grandson1 #xz-main-content-warp1-grandson2").eq(i).animate({
							bottom: "-107"
						}, 300)
					})

			})

	})

	

	//动态获取content-son
	$(function() {
		$.getJSON("xz.json", function(json) {
			$(".xz-main-content-warp1-son").each(function(i) {
				$(".xz-main-content-warp1-son img").eq(i).attr({
					"src": json.main_son1[i].son1_photo
				})
				$(".xz-main-content-warp1-son h4 a").eq(i).html(json.main_son1[i].main_son1_h4_font);
				$(".xz-main-content-warp1-son .xz-content-salepirce-now").eq(i).html(json.main_son1[i].content_salepirce_now);
				$(".xz-main-content-warp1-son .xz-content-salepirce-old").eq(i).html(json.main_son1[i].content_salepirce_old);
				$(".xz-main-content-warp1-son").eq(i).attr({
					"id": json.main_son1[i].content_id
				});
			})
		})
	})

	//第二部分内容
	$(function() {
		$.getJSON("xz.json", function(json) {
			for(var i = 0; i < 17; i++) {
				$("<a>", {
					"class": "xz-main-content-warp2-grandson"
				}).appendTo("#xz-main-content-warp2-son");
				$("<img>").attr({
						"src": json.content_son2_photo[i].content_warp2_son1
					})
					.appendTo(".xz-main-content-warp2-grandson").eq(i);
			}
		})
	})

 

	//悬浮菜单一

	$("#xz-menu1 li").each(function(i) {
		$("#xz-menu1 li").eq(i).hover(function() {
				$("#xz-menu1 li a").eq(i).css({
					"display": "block"
				})
			},
			function() {
				$("#xz-menu1 li a").eq(i).css({
					"display": "none"
				})
			}
		)
	})

	$(function() {
		var top = document.documentElement.scrollTop || document.body.scrollTop;
		$("#xz-main-content").mouseover(function() {
				var top = document.documentElement.scrollTop || document.body.scrollTop;
				if(top >= 450) {
					$("#xz-menu1").css({
						"display": "block"
					})
				} else {
					$("#xz-menu1").css({
						"display": "none"
					})
				}
			}),
			$("#xz-main-content").mouseout(function() {
				$("#xz-menu1").css({
					"display": "none"
				})
			})
		$("#xz-bottom").mouseover(function() {
			$("#xz-menu1").css({
				"display": "block"
			})
		})

	})
	//悬浮菜单二
	$(function() {
		$(function() {
			var top = document.documentElement.scrollTop || document.body.scrollTop;
			$("#xz-main-content").mouseover(function() {
					var top = document.documentElement.scrollTop || document.body.scrollTop;
					if(top >= 450) {
						$("#xz-menu2").css({
							"display": "block"
						})
					} else {
						$("#xz-menu2").css({
							"display": "none"
						})
					}
				}),
				$("#xz-main-content").mouseout(function() {
					$("#xz-menu2").css({
						"display": "none"
					})
				})
			$("#xz-bottom").mouseover(function() {
				$("#xz-menu2").css({
					"display": "block"
				})
			})
		})
	})
	//跳转到详情页
	$(function() {
		$(".xz-main-content-warp1-son img").each(function(i) {
			$(".xz-main-content-warp1-son img").eq(i).mousedown(function() {
				$.cookie("product", i,{expires:7,path:"/"});
				location.href = "html/productdetail.html";
			})

		})

	})
	//添加商品到购物车
	$(function() {

		$(".xz-content-but").each(function(i) {
			$(".xz-content-but").eq(i).mousedown(function() {

				$.getJSON("xz.json", function(obj) {
					var goods = $.cookie("goods") ? $.cookie("goods") : "";
					var id = $(".xz-main-content-warp1-son").eq(i).attr("id");
					var obj1 = converStrToObj(goods);

					if(id in obj1) {
						obj1[id].num = parseInt(obj1[id].num) + 1;

					} else {
						obj1[id] = {
							name: obj.main_son1[i].main_son1_h4_font,
							pirce: obj.main_son1[i].content_salepirce_now,
							integral: obj.main_son1[i].content_integral,
							imgsrc: "../" + obj.main_son1[i].son1_photo,
							num: 1
						}
					}
					$.cookie("goods", convertObjTostr(obj1), {
						expires: 7,
						path: "/"
					});
					alert("已添加至购物车")
				var goods = converStrToObj($.cookie("goods"));
				var goodsnum = 0;
				for(val in goods) {
					goodsnum += parseInt(goods[val].num);
				}
				$(".shopcar").text(goodsnum);
					
					
				})
			
			})

		})
	})

	//字符串转对象
	function converStrToObj(str) {

		var obj = {};
		if(!str) {
			return {};
		}
		var arr = str.split(":");
		for(var i = 0; i < arr.length; i++) {
			arr2 = arr[i].split(",");
			obj[arr2[0]] = {
				name: arr2[1],
				pirce: arr2[2],
				num: arr2[3],
				integral: arr2[4],
				imgsrc: arr2[5]

			}
		}
		return obj;
	}
	//对象转字符串
	function convertObjTostr(obj) {
		var str = "";
		for(val in obj) {
			if(str) {
				str += ":";
			}
			str += val + "," + obj[val].name + "," + obj[val].pirce + "," + obj[val].num + "," +
				obj[val].integral + "," + obj[val].imgsrc
		}
		return str;
	}

})